package com.azt.front.action.company.shop;

import com.azt.api.dto.item.ProductExt;
import com.azt.api.dto.shop.ProCategory;
import com.azt.api.dto.shop.ProSearchParam;
import com.azt.api.pojo.*;
import com.azt.api.service.AreaService;
import com.azt.api.service.CompanyService;
import com.azt.api.service.ProCategoryService;
import com.azt.api.service.ProductService;
import com.azt.front.permission.RequireProvider;
import com.azt.front.utils.ContextUtil;
import com.azt.model.AjaxJson;
import com.azt.model.page.TPagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 店铺
 * Created by 张栋 on 2017/2/617:28
 */
@Controller
@RequestMapping("/shop")
public class ShopAction {

    @Autowired
    private CompanyService companyService;


    @Autowired
    private ProductService productService;


    @Autowired
    private ProCategoryService proCategoryService;

    @Autowired
    private AreaService  areaService;
    /**
     * 店铺首页
     * Created by 张栋 on 2017/2/8 11:32
     */
    @RequestMapping("/{shopid}")
    public String shopIndex(ModelMap model, @PathVariable("shopid")Integer shopid){
        return "forward:/shop/"+shopid+"/0";
    }

    @RequestMapping("/company/{companyId}")
    public String shopIndex2(@PathVariable("companyId") Integer companyId) {
        CompanyExt ext = companyService.getCompanyExtByCustomerid(companyId);
        if(ext != null) {
            return "redirect:/shop/" + ext.getId();
        }
        return "404";
    }

    //shopid  指的是  u_company_ext 里的  id
    @RequestMapping("/{shopid}/{catid}")
    public String shopIndex(ProSearchParam param, ModelMap model,@PathVariable("shopid")Integer shopid,@PathVariable("catid")Integer categoryid){
        param.setPageSize(16);
        //判断店铺是否存在
        CompanyExt shop = companyService.getCompanyExtById(shopid);
        if(shop==null){
            return "redirect:/";
        }

        //判断公司是否存在
        Company company = companyService.getCompanyById(shop.getCompanyId());
        param.setCategoryIds(String.valueOf(categoryid));
        param.setCompanyId(company.getId());

        //获取公司详情
        CompanyInfo info = companyService.getCompanyInfoByCompanyId(company.getId());


        //获取产品相关类目与数量
        List<ProCategory> cats = productService.getProCategory(company.getId(),categoryid);


        //获取检索到的产品
        TPagination products = productService.getProductList(param);

        //获取热门推荐
        ProSearchParam proSearchParam = new ProSearchParam();
        proSearchParam.setCompanyId(company.getId());
        proSearchParam.setPageNo(1);
        proSearchParam.setPageSize(3);
        proSearchParam.setOrderType(6);//浏览量desc
        TPagination<ProductExt> hots = productService.getProductList(proSearchParam);

        model.put("currentCat",categoryid);//当前的类目id
        model.put("shop",shop);
        model.put("company",company);
        model.put("info",info);
        model.put("cats",cats);//类目与数量
        model.put("pagination",products);//产品列表
        model.put("hots",hots);//热门推荐
        model.put("param",param);//搜索内容

        return "shop/shop_index";
    }



    /**
     * 店铺设置
     * Created by 张栋 on 2017/3/2 15:54
     *
     */
    @RequestMapping("/setting")
    @RequireProvider
    public String setting( ModelMap model){
        User user = ContextUtil.getUser();
        Company company = ContextUtil.getCompany();
        CompanyInfo info = companyService.getCompanyInfoByCompanyId(company.getId());
        CompanyExt ext = companyService.getCompanyExtByCustomerid(info.getCompanyId());
        if(ext==null){
            ext = new CompanyExt();
            ext.setCompanyId(info.getCompanyId());
            ext.setAddress(info.getBusinessAddress());
            ext.setProvince(info.getBusinessProvince());
            ext.setCity(info.getBusinessCity());
            ext.setArea(info.getBusinessArea());
//            ext.setCantactName(user.getRealName());
//            ext.setCantactTel(user.getMobile());
//            ext.setGender(user.getGender());
            ext.setDisplay(1);
        }
        model.put("ext",ext);
        return "shop/shop_setting";
    }



    /**
     * 新增与编辑
     * Created by 张栋 on 2017/3/3 13:28
     */
    @RequestMapping("/upsetting")
    @ResponseBody
    public AjaxJson upSetting(ModelMap model,CompanyExt ext){
        AjaxJson j = new AjaxJson();
        String province = ext.getProvince();
        String city = ext.getCity();
        String area = ext.getArea();
        ext.setProvinceName(areaService.getAreaByCode(province).getName());
        ext.setCityName(areaService.getAreaByCode(city).getName());
        ext.setAreaName(areaService.getAreaByCode(area).getName());


        if(ext.getId()==null){
            companyService.saveCompanyExt(ext);
        }else{
            companyService.updateCompanyExt(ext);
        }
        j.setObj(ext.getId());
        return j;
    }



}
